package com.huike.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.huike.clues.domain.SysDictType;
import com.huike.clues.domain.dto.SysDictTypeAddDTO;
import com.huike.clues.domain.dto.SysDictTypeQueryDTO;
import com.huike.clues.domain.result.PageResult;
import com.huike.clues.service.ISysDictTypeService;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.domain.entity.SysDictTypeDTO;
import com.huike.common.core.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/dict/type")
public class SysDictTypeController extends BaseController {
    @Autowired
    private ISysDictTypeService sysDictTypeService;

    @GetMapping("/list")
    public TableDataInfo list(SysDictTypeQueryDTO dto) {
        startPage();
        List<SysDictType> list = sysDictTypeService.selectList(dto);

        return getDataTable(list);
    }
    @GetMapping("/{dictId}")
    public AjaxResult<SysDictType> get(@PathVariable("dictId") Long dictId) {

        return AjaxResult.success(sysDictTypeService.getById(dictId));
    }
    @PostMapping
    public AjaxResult save(@RequestBody SysDictTypeAddDTO dto) {
        LambdaQueryWrapper<SysDictType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysDictType::getDictType, dto.getDictType());
         if (sysDictTypeService.getOne(queryWrapper)!=null) {
             throw new RuntimeException("新增字典"+dto.getDictName()+"失败,字典类型已存在");
         }
        sysDictTypeService.insert(dto);
        return AjaxResult.success();
    }
}
